using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using Elev.Core.DAL;
using Elev.Core.Utility;

namespace Elev.Core
{
    public class Exam : IDimension
    {
        int id;
        string name;
        DateTime date;
        int periodID;
        int examTypeID;
        int coefficient;
        string note;

        public int ID { get { return id; } }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        public int PeriodID
        {
            get { return periodID; }
            set { periodID = value; }
        }

        public Period Period
        {
            get
            {
                if (periodID > 0)
                {
                    PeriodsFactory factory = new PeriodsFactory();
                    return factory.GetPeriod(periodID);
                }
                return null;
            }
        }

        public int ExamTypeID
        {
            get { return examTypeID; }
            set { examTypeID = value; }
        }

        public ExamType ExamType
        {
            get
            {
                if (examTypeID > 0)
                {
                    ExamTypesFactory factory = new ExamTypesFactory();
                    return factory.GetExamType(examTypeID);
                }
                return null;
            }
        }

        public int Coefficient
        {
            get { return coefficient; }
            set { coefficient = value; }
        }

        public string Note
        {
            get { return note; }
            set { note = value; }
        }

        public Exam(DataRow row)
        {
            parse(row);
        }

        public Exam() : this(-1, "", DateTime.MinValue, -1, -1, 1, "") { }

        public Exam(int ID, string Name, DateTime Date, int PeriodID, int ExamTypeID, int Coefficient, string Note)
        {
            id = ID;
            name = Name;
            date = Date;
            periodID = PeriodID;
            examTypeID = ExamTypeID;
            coefficient = Coefficient;
            note = Note;
        }

        void parse(DataRow dr)
        {
            id = Convert.ToInt32(dr["ID"]);
            name = (string)dr["Name"];
            date = DateTools.FormatDate((string)dr["Date"]);
            periodID = Convert.ToInt32(dr["PeriodID"]);
            examTypeID = Convert.ToInt32(dr["ExamTypeID"]);
            coefficient = Convert.ToInt32(dr["Coefficient"]);
            note = (string)dr["Note"];
        }

        #region IDimension Members

        public int Save()
        {
            ExamInternal examInternal = new ExamInternal();
            if (id == -1)
            {
                // New creation ...
                id = examInternal.Add(name, date, periodID, examTypeID, coefficient, note);
            }
            else
            {
                examInternal.Update(id, name, date, periodID, examTypeID, coefficient, note);
            }
            return id;
        }

        public void Delete()
        {
            ExamInternal examInternal = new ExamInternal();
            examInternal.Delete(id);
        }

        #endregion

        public override string ToString()
        {
            return name;
        }

        public string ToToolTipString()
        {
            StringBuilder str = new StringBuilder();
            str.Append(ExamType.Name);
            str.AppendLine();
            str.AppendFormat("Coeff. {0}", coefficient);
            str.AppendLine();
            str.Append(note);

            return str.ToString();
        }
    }
}
